﻿Ext.data.AjaxProxy = Ext.extend(Ext.data.ServerProxy, {

    //HTTP请求方式
    actionMethods: {
        create: 'POST',
        read: 'GET',
        update: 'POST',
        destroy: 'POST'
    },



    constructor: function () {
        this.addEvents(

            'exception'
        );

        Ext.data.AjaxProxy.superclass.constructor.apply(this, arguments);
    },

    //在ServerProxy的子类中,create, read, update 和 destroy 方法都委托于doRequest方法。
    //Each ServerProxy subclass must implement the doRequest method - 
    //每个ServerProxy的子类都必须实现doRequest方法。
    doRequest: function (operation, callback, scope) {
        var writer = this.getWriter(), //获取当前代理的Writer实例
            request = this.buildRequest(operation, callback, scope); //创建并返回一个Ext.data.Request对象

        if (operation.allowWrite()) {//检查当前操作是否应该引发回写操作。
            request = writer.write(request); //将operation中的记录以writer默认的形式（json或xml）赋给request
        }

        Ext.apply(request, {
            headers: this.headers, //添加给请求的任何头部信息。默认为undefined。
            timeout: this.timeout, //请求响应的等待时间。 默认为30秒.
            scope: this, 
            callback: this.createRequestCallback(request, operation, callback, scope),//回调函数
            method: this.getMethod(request), //返回请求的HTTP方式名。
            disableCaching: false
        });

        Ext.Ajax.request(request);//发送请求

        return request;
    },

    //返回请求的HTTP方式名。
    getMethod: function (request) {
        return this.actionMethods[request.action];
    },

    //创建请求回调函数
    createRequestCallback: function (request, operation, callback, scope) {
        var me = this;

        //返回回调函数
        return function (options, success, response) {
            //如果调用服务成功
            if (success === true) {
                var reader = me.getReader(), //获取当前代理的Reader实例
                    //读取给定的response对象，并且在将记录传递给readRecords方法之前将不同类型的response对象标准化
                    result = reader.read(response), 
                    records = result.records,
                    length = records.length,
                    mc = new Ext.util.MixedCollection(true, function (r) { return r.getId(); }),
                    record, i;

                mc.addAll(operation.records);
                for (i = 0; i < length; i++) {
                    record = mc.get(records[i].getId());

                    if (record) {
                        record.set(record.data);
                    }
                }


                Ext.apply(operation, {
                    response: response,
                    resultSet: result
                });

                operation.setCompleted();
                operation.setSuccessful();
            } 
            else {//如果调用服务失败
                me.fireEvent('exception', this, response, operation);


                operation.setException();
            }


            if (typeof callback == 'function') {
                callback.call(scope || me, operation);//运行回调函数
            }

            me.afterRequest(request, true);
        };
    }
});

Ext.data.ProxyMgr.registerType('ajax', Ext.data.AjaxProxy);


Ext.data.HttpProxy = Ext.data.AjaxProxy;